<template>
  <div style="padding: 20px">
    <el-row :gutter="20">
      <el-col :span="12">
        <div id="bar-chart" class="chart-container"></div>
      </el-col>
      <el-col :span="12">
        <div id="pie-chart" class="chart-container"></div>
      </el-col>
    </el-row>
    <el-row :gutter="20" style="margin-top: 20px">
      <el-col :span="24">
        <div id="line-chart" class="chart-container"></div>
      </el-col>
    </el-row>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import { listStock, getStock, delStock, addStock, updateStock } from '@/api/system/stock'

export default {
  data() {
    return {
      loading: true,
      ids: [],
      single: true,
      multiple: true,
      showSearch: true,
      total: 0,
      stockList: [],
      title: '',
      open: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        materialId: null,
        quantity: null,
        action: null,
        createdAt: null,
        updatedAt: null
      },
      form: {},
      rules: {
        materialId: [{ required: true, message: '物料id不能为空', trigger: 'blur' }],
        quantity: [{ required: true, message: '数量不能为空', trigger: 'blur' }],
        action: [{ required: true, message: '入库/出库不能为空', trigger: 'blur' }]
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询库存管理列表 */
    getList() {
      this.loading = true
      listStock(this.queryParams)
        .then(response => {
          this.stockList = response.rows
          this.total = response.total
          this.loading = false
          this.drawCharts()
        })
        .catch(error => {
          console.error('Error fetching stock data:', error)
          this.loading = false
        })
    },
    drawCharts() {
      const categories = this.stockList.map(item => item.materialName)
      const quantities = this.stockList.map(item => item.quantity)

      this.drawBarChart(categories, quantities)
      this.drawPieChart(categories, quantities)
      this.drawLineChart(categories, quantities)
    },
    drawBarChart(categories, quantities) {
      var chart = echarts.init(document.getElementById('bar-chart'))

      var option = {
        title: {
          text: '库存柱状图'
        },
        tooltip: {},
        legend: {
          data: ['库存']
        },
        xAxis: {
          data: categories
        },
        yAxis: {},
        series: [
          {
            name: '库存',
            type: 'bar',
            data: quantities
          }
        ]
      }

      chart.setOption(option)
    },
    drawPieChart(categories, quantities) {
      var chart = echarts.init(document.getElementById('pie-chart'))

      var option = {
        title: {
          text: '库存饼图'
        },
        tooltip: {
          trigger: 'item'
        },
        legend: {
          top: '5%',
          left: 'center'
        },
        series: [
          {
            name: '库存',
            type: 'pie',
            radius: '50%',
            data: categories.map((category, index) => ({ value: quantities[index], name: category })),
            emphasis: {
              itemStyle: {
                shadowBlur: 10,
                shadowOffsetX: 0,
                shadowColor: 'rgba(0, 0, 0, 0.5)'
              }
            }
          }
        ]
      }

      chart.setOption(option)
    },
    drawLineChart(categories, quantities) {
      var chart = echarts.init(document.getElementById('line-chart'))

      var option = {
        title: {
          text: '库存折线图'
        },
        tooltip: {
          trigger: 'axis'
        },
        legend: {
          data: ['库存']
        },
        xAxis: {
          type: 'category',
          data: categories
        },
        yAxis: {
          type: 'value'
        },
        series: [
          {
            name: '库存',
            type: 'line',
            data: quantities
          }
        ]
      }

      chart.setOption(option)
    }
  }
}
</script>

<style scoped>
.chart-container {
  width: 100%;
  height: 400px;
  padding: 20px;
  border: 1px solid #ddd;
  background: #fff;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.1);
  border-radius: 10px;
}
</style>
